package com.hbnu.pojo;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 用户实体类
 * @author system
 * @since 2024
 */
@TableName("user")
public class User implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 用户ID - 主键
     */
    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;
    
    /**
     * 微信openid - 唯一标识
     */
    @TableField("openid")
    private String openid;
    
    /**
     * 用户名
     */
    @TableField("username")
    private String username;
    
    /**
     * 头像URL
     */
    @TableField("avatarUrl")
    private String avatarUrl;
    
    /**
     * 微信会话密钥（敏感信息，不返回给前端）
     */
    @TableField("sessionKey")
    @JsonIgnore
    private String sessionKey;
    
    /**
     * 最后登录时间
     */
    @TableField(value = "last_login_time", fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime lastLoginTime;
    
    /**
     * 创建时间
     */
    @TableField(value = "created_time", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createdTime;
    
    /**
     * 用户总分
     */
    @TableField("total_score")
    private Integer totalScore;
    
    // ==================== 构造函数 ====================
    
    /**
     * 默认构造函数
     */
    public User() {
        this.totalScore = 0; // 默认分数为0
    }
    
    /**
     * 基础构造函数
     * @param openid 微信openid
     * @param sessionKey 会话密钥
     */
    public User(String openid, String sessionKey) {
        this();
        this.openid = openid;
        this.sessionKey = sessionKey;
        this.createdTime = LocalDateTime.now();
        this.lastLoginTime = LocalDateTime.now();
    }
    
    /**
     * 完整构造函数
     * @param openid 微信openid
     * @param username 用户名
     * @param avatarUrl 头像URL
     * @param sessionKey 会话密钥
     */
    public User(String openid, String username, String avatarUrl, String sessionKey) {
        this(openid, sessionKey);
        this.username = username;
        this.avatarUrl = avatarUrl;
    }
    
    // ==================== Getter和Setter方法 ====================
    
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public String getOpenid() {
        return openid;
    }
    
    public void setOpenid(String openid) {
        this.openid = openid;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getAvatarUrl() {
        return avatarUrl;
    }
    
    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }
    
    public String getSessionKey() {
        return sessionKey;
    }
    
    public void setSessionKey(String sessionKey) {
        this.sessionKey = sessionKey;
    }
    
    public LocalDateTime getLastLoginTime() {
        return lastLoginTime;
    }
    
    public void setLastLoginTime(LocalDateTime lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }
    
    public LocalDateTime getCreatedTime() {
        return createdTime;
    }
    
    public void setCreatedTime(LocalDateTime createdTime) {
        this.createdTime = createdTime;
    }
    
    /**
     * 为了兼容前端，添加createTime的getter方法
     * @return 创建时间
     */
    public LocalDateTime getCreateTime() {
        return createdTime;
    }
    
    public Integer getTotalScore() {
        return totalScore;
    }
    
    public void setTotalScore(Integer totalScore) {
        this.totalScore = totalScore;
    }
    
    // ==================== 业务方法 ====================
    
    /**
     * 更新登录时间
     */
    public void updateLoginTime() {
        this.lastLoginTime = LocalDateTime.now();
    }
    
    /**
     * 增加分数
     * @param score 要增加的分数
     */
    public void addScore(Integer score) {
        if (score != null && score > 0) {
            this.totalScore = (this.totalScore == null ? 0 : this.totalScore) + score;
        }
    }
    
    /**
     * 重置分数
     */
    public void resetScore() {
        this.totalScore = 0;
    }
    
    /**
     * 检查用户信息是否完整
     * @return true表示信息完整
     */
    public boolean isProfileComplete() {
        return openid != null && !openid.trim().isEmpty() 
               && username != null && !username.trim().isEmpty();
    }
    
    // ==================== Object方法重写 ====================
    
    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", openid='" + openid + '\'' +
                ", username='" + username + '\'' +
                ", avatarUrl='" + avatarUrl + '\'' +
                ", lastLoginTime=" + lastLoginTime +
                ", createdTime=" + createdTime +
                ", totalScore=" + totalScore +
                '}';
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        User user = (User) obj;
        return userId != null && userId.equals(user.userId);
    }
    
    @Override
    public int hashCode() {
        return userId != null ? userId.hashCode() : 0;
    }
}
